//<!--job单页面-->
var url = getApiUrl('http://www.jiaoyou0.cn', 'user/userEdit');
var Strarr = [];
var main = plus.webview.getWebviewById('My');
var Str = document.getElementById("job_item").getElementsByTagName('li');
for(var i = 0; i < Str.length; i++) {
	Strarr.push(Str[i].getElementsByTagName('a'))
}

for(var j = 0; j < Strarr.length; j++) {
	if(Strarr[j][0].innerText == user_info.job) {
		Strarr[j][0].classList.add('right_bg');
	}
	Strarr[j][0].addEventListener('tap', function() {
		for(var h = 0; h < Strarr.length; h++) {
			if(Strarr[h][0].classList.contains('right_bg')) {
				Strarr[h][0].classList.remove('right_bg');
			}
		}
		this.classList.add('right_bg');
		intro_value = this.innerHTML;
		DataAjax('job', intro_value);
	})

}
//<!--job  End-->
//<!--个性签名单页面-->
function getDefault() {
	return {
		user_id: '',
		intro: '',
		length: ''
	}
}
;
var vp = new Vue({
	el: '.idiograph-text',
	data: getDefault()
});
if(user_info.intro != '') {
	vp.length = user_info.intro.length
} else {
	vp.length = document.getElementById("txta1").value.length
}

vp.intro = user_info.intro;
document.getElementById("txta1").oninput = function() {
	vp.length = this.value.length;
};

document.getElementById("intro_compile").addEventListener('tap', function() {
	mui(this).button('loading');
	setTimeout(function() {
		mui(this).button('reset');
		stage();
		var value = mui('.idiograph-text .idiograph-input')[0].value;
		DataAjax('intro', value);
	}.bind(this), 1000);

})
//<!--intro  End-->


////返回

document.getElementById("intro_back").addEventListener('tap', function() {
	mui.back();
})

//<!--单页面-->
var viewApi = mui('#app').view({
	defaultPage: '#compile'
});
//初始化单页的区域滚动
mui('.mui-scroll-wrapper').scroll();
var view = viewApi.view;
(function($) {
	var self = plus.webview.currentWebview();
	$.back = function() {
		plus.webview.close(self, 'fade-out');
	}
	//处理view的后退与webview后退
	var oldBack = $.back;
	$.back = function() {
		if(viewApi.canBack()) { //如果view可以后退，则执行view的后退
			viewApi.back();
		} else { //执行webview后退
			oldBack();
		}
	};

	view.addEventListener('pageBeforeShow', function(e) {

	});
	view.addEventListener('pageShow', function(e) {

	});
	view.addEventListener('pageBeforeBack', function(e) {

	});
	view.addEventListener('pageBack', function(e) {

	});
})(mui);

//mui初始化
mui.init({
	swipeBack: false //启用右滑关闭功能
});

function getDefaultData() {
	return {
		user_id: '',
		nick_name: '',
		intro: '',
		star: '',
		height: '',
		job: '可选',
		address: '',
		year: '',
	}
}
var vm = new Vue({
	el: '#compile',
	data: getDefaultData(),
	methods: {
		resetData: function() {
			Object.assign(this.$data, getDefaultData())
		},
		doName: function() {
			var bnt = ['取消', '确认']
			stage();  
			mui.prompt('', user_info.nick_name, '修改昵称', bnt, function(e) {
				if(e.index == 1) {
					var value = mui('.mui-popup-input input')[0].value;
					if( value != ''){
						DataAjax('nick_name', value)
					}
				}
			}, 'div')
			document.querySelector('.mui-popup-input input').value = user_info.nick_name;
		}

	},
	filters: {
		qianming: function(value) {
			if(value == '') {
				return '写个签名吧！'
			} else {
				return value
			}  
		},
		yearchu: function(value) {
			if(value < 1000) {
				return '可选择'
			} else {
				var nowyear = new Date().getFullYear();
				return (nowyear-value)+'岁'
			}
		}
	}
});

function Job() {

	//拉取本地存储
	stage();
	vm.user_id = user_info.user_id;
	vm.nick_name = user_info.nick_name;
	if(user_info.height == 0) {
		user_info.height = '未填写'
	}else{
		user_info.height = user_info.height + 'CM'
	}
	vm.height = user_info.height 
	if(user_info.job == "") {
		user_info.job = '未填写'
	}
	vm.job = user_info.job;
	if(user_info.address == "") {
		user_info.address = '未填写'
	}

	vm.address = user_info.address;
	if(user_info.year == 0) {
		user_info.star = '未选择年龄';
	}
	vm.year = user_info.year;
	vm.star = user_info.star;
	vm.intro = user_info.intro;
}
Job();

//原生日期选择

var age = document.getElementsByClassName("age")[0];
var toDay = new Date().getFullYear();
document.getElementById("age").addEventListener('tap', function() {
	//拉取本地存储
	stage();
	var dDate = new Date();
	dDate.setFullYear(1999, 6, 16);
	if(user_info.years && user_info.months && user_info.dates) {
		dDate.setFullYear(user_info.years, user_info.months - 1, user_info.dates);
	}
	var minDate = new Date();
	minDate.setFullYear(1964, 1, 1);
	var maxDate = new Date();
	maxDate.setFullYear(1999, 1, 1);
	plus.nativeUI.pickDate(function(e) {
		//选择完的回调
		var d = e.date;
		years = d.getFullYear();
		months = d.getMonth() + 1;
		dates = d.getDate();
		user_info.years = years;
		user_info.months = months;
		user_info.dates = dates;
		var value = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
		age.innerText = toDay - d.getFullYear() + '岁';
		DataAjax('birthday', value, d)
	}, function(e) {

	}, {
		title: "请选择日期",
		date: dDate,
		minDate: minDate,
		maxDate: maxDate
	});
})

var heightPicker = new mui.PopPicker();
heightPicker.setData([{
	text: '150CM'
}, {
	text: '151CM'
}, {
	text: '152CM'
}, {
	text: '153CM'
}, {
	text: '154CM'
}, {
	text: '155CM'
}, {
	text: '156CM'
}, {
	text: '157CM'
}, {
	text: '158CM'
}, {
	text: '159CM'
}, {
	text: '160CM'
}, {
	text: '161CM'
}, {
	text: '162CM'
}, {
	text: '163CM'
}, {
	text: '164CM'
}, {
	text: '165CM'
}, {
	text: '166CM'
}, {
	text: '167CM'
}, {
	text: '168CM'
}, {
	text: '169CM'
}, {
	text: '170CM'
}, {
	text: '171CM'
}, {
	text: '172CM'
}, {
	text: '173CM'
}, {
	text: '174CM'
}, {
	text: '175CM'
}, {
	text: '176CM'
}, {
	text: '177CM'
}, {
	text: '178CM'
}, {
	text: '179CM'
}, {
	text: '180CM'
}, {
	text: '181CM'
}, {
	text: '182CM'
}, {
	text: '183CM'
}, {
	text: '184CM'
}, {
	text: '185CM'
}, {
	text: '186CM'
}, {
	text: '187CM'
}, {
	text: '188CM'
}, {
	text: '189CM'
}, {
	text: '190CM'
}, {
	text: '191CM'
}, {
	text: '192CM'
}, {
	text: '193CM'
}, {
	text: '194CM'
}, {
	text: '195CM'
}, {
	text: '196CM'
}, {
	text: '197CM'
}, {
	text: '198CM'
}, {
	text: '200CM'
}]);
var heightClick = document.getElementById('height');
var heightResult = document.getElementById('heightResult');
heightClick.addEventListener('tap', function(event) {
	//拉取本地存储
	stage();
	heightPicker.show(function(items) {
		heightResult.innerText = items[0].text;
		var height = heightResult.innerHTML;
		DataAjax('height', height)
	})
}, false);
// 城市联动————2级联示例
var cityPicker = new mui.PopPicker({
	layer: 2
});
cityPicker.setData(cityData);
var cityResult = document.getElementById('cityResult');
document.getElementById("address").addEventListener('tap', function(event) {
	//拉取本地存储
	stage();
	cityPicker.show(function(items) {
		cityResult.innerText = items[1].text;
		var Position = cityResult.innerText;
		DataAjax('address', Position)
	});
}, false);
//重新获取本地存储
function stage() {
	var info = plus.storage.getItem("user_info");

	if(info) {
		user_info = JSON.parse(info);
		configs.user_id = user_info.user_id;
	}
}
//上传数据
function DataAjax(type, value, other) {
	mui.ajax({
		type: "post",
		url: url,
		async: true,
		data: {
			'param': type,
			'value': value
		},
		success: function(data) {
			var data = JSON.parse(data);
			if(data.code == 1) {
				if(type == 'address') {
					user_info.address = value;
				} else if(type == 'height') {
					user_info.height = value;
				} else if(type == 'birthday') {
					age.innerText = toDay - other.getFullYear() + '岁';
					vm.star = data.data.xingzuo;
					user_info.year = other.getFullYear();
					user_info.star = data.data.xingzuo;
				} else if(type == 'nick_name') {
					vm.nick_name = value;
					user_info.nick_name = value;
				} else if(type == 'intro') {
					vm.intro = value;
					user_info.intro = value;
					mui.back()
				} else if(type == 'job') {
					vm.job = value;
					user_info.job = value;
					mui.back()
				}
				plus.storage.setItem('user_info', JSON.stringify(user_info));
				if(type == 'nick_name'){
					mui.fire(main, 'myupdate');
				}
			} else if(data.code == 0) {
				if(type == 'intro') {
					mui.back();
				}
				if(type == 'job') {
					mui.back();
				}
				
			}
		},
		error: function(data) {
			mui.toast('修改失败,请稍候重试');
			console.log(JSON.stringify(data))
		}
	})
}